﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace ThreadTest
{
    public partial class Form1 : Form
    {
        private CommonTimer timer = new CommonTimer();
        private delegate void UpdateCount(int count);
        private Thread thread;

        public Form1()
        {
            InitializeComponent();
            timer.OnCountChange += new CommonTimer.CountChange(timer_OnCountChange);

            thread = new Thread(new ThreadStart(timer.DoWord));
            thread.IsBackground = true;
        }

        private void timer_OnCountChange(int count)
        {
            this.Invoke(new UpdateCount(NotifyCount), count);
        }

        private void NotifyCount(int count)
        {
            this.lbDisplay.Text = count.ToString();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (thread.ThreadState == (ThreadState.Background | ThreadState.Suspended))

                try
                {
                    thread.Resume();
                }
                catch
                {
                }
                finally
                {
                }
            else
                thread.Start();


        }

        private void button2_Click(object sender, EventArgs e)
        {
            thread.Suspend();
        }
    }
}
